package net.butfly.bus.filter;

import net.butfly.bus.Request;
import net.butfly.bus.Response;
import net.butfly.bus.config.Config;
import net.butfly.bus.context.Context;
import net.butfly.bus.context.FlowNo;
import net.butfly.bus.invoker.InvokerFactory;
import net.butfly.bus.policy.Router;

public class InvokerFilter extends FilterBase implements Filter {
	private Config config;
	private Router router;

	public InvokerFilter(Config config, Router router) {
		super();
		this.config = config;
		this.router = router;
	}

	@Override
	public void execute(Request request, Response response) throws Exception {
		Context.merge(Context.deserialize(request.context()));
		new FlowNo(request);
		InvokerFactory.getInvoker(config.getNodeBean(router.route(request.code()))).invoke(response, request);
		response.context(Context.serialize(Context.toMap()));
	}
}
